package minh.app.mbook;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MBookView extends View {
	// MyHandler myHandler = new MyHandler();
	private static int DEFAULT_FLIP_VALUE = 20;
	private static int FLIP_SPEED = 30;
    
	private long mMoveDelay = 1000 / 30;

	float xTouchValue = DEFAULT_FLIP_VALUE, yTouchValue = DEFAULT_FLIP_VALUE;;
    
	private int count = 0;
	Paint paint = new Paint();
	// String result = "";

	//
	class FlippingHandler extends Handler 
	{
		@Override
		public void handleMessage(Message msg) 
		{
			Log.i("Thong bao: ", "Clock Handler is still running");
			if(count == 1)
			{
				MBookView.this.flip(visiblePage, invisiblePage, visibleView, invisibleView);
				
			}
			else
			if(count == 2)
			{
				MBookView.this.flip(invisiblePage, invisiblePage2, invisibleView, invisibleView2);
				
			}
			else
			if(count == 3)
			{
				MBookView.this.flip(invisiblePage2, visiblePage, invisibleView2, visibleView);
				
			}
		}

		public void sleep(long delayMillis) 
		{

			this.removeMessages(0);

			sendMessageDelayed(obtainMessage(0), delayMillis);
			

		}
	}

	//

	//
	FlippingHandler flippingHandler;
	//
	int width;
	int height;

	float oldTouchX, oldTouchY;
	boolean flipping = false;
	boolean next;

	Point A, B, C, D, E, F;

	Bitmap visiblePage;
	Bitmap invisiblePage;
	Bitmap invisiblePage2;
	Paint flipPagePaint;
	
	Bitmap drawtemp1;
	Bitmap drawtemp2;

	boolean flip = false;

	Context context;
	//
	int loadedPages = 0;
	long timeToLoad = 0;
	//

	// boolean loadingDone = false;

	boolean onloading = true;

	boolean onMoving = false;

	

	public MBookView(Context context, int width, int height) {
		super(context);
		this.context = context;
		this.width = width;
		this.height = height;
		//
		//page1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.tifa);
		//page2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.cloud);
		//

		//init(page1, page2);
	}

	//

	//
	public void init(Bitmap page1, Bitmap page2, Bitmap page3) 
	{

		//
		flippingHandler = new FlippingHandler();
		//
		flipPagePaint = new Paint();
		flipPagePaint.setColor(Color.GRAY);
		// flipPagePaint.setAntiAlias(true);
		// flipPagePaint.setStyle(Paint.Style.FILL);
		flipPagePaint.setShadowLayer(5, -5, 5, 0x99000000);
		A = new Point(10, 0);
		B = new Point(width, height);
		C = new Point(width, 0);
		D = new Point(0, 0);
		E = new Point(0, 0);
		F = new Point(0, 0);

		xTouchValue = yTouchValue = DEFAULT_FLIP_VALUE;
		visiblePage = page1;
		invisiblePage = page2;
		invisiblePage2 = page3;
		onMoving = false;
		flipping = false;

		//

		//
		loadData();

	}

	//
	private void loadData() {
		// listOfPages.add("this is my text");
		onloading = false;

	}
	//
	private View visibleView, invisibleView, invisibleView2;
	//
	public void setView(View view1, View view2, View view3){
		visibleView = view1;
		invisibleView = view2;
		invisibleView2 = view3;
		visibleView.setVisibility(VISIBLE);
		invisibleView.setVisibility(GONE);
		invisibleView2.setVisibility(GONE);
		
		this.setVisibility(GONE);
	}

	//
	public void next()
	{
		if(visibleView.getVisibility() == View.VISIBLE)
		{
			visibleView.setVisibility(GONE);
			this.setVisibility(VISIBLE);
			flipping = true;
			next = true;		
			drawtemp1 = visiblePage;
			drawtemp2 = invisiblePage;
			flip(visiblePage, invisiblePage, visibleView, invisibleView);
			count =1;
		}
		else
		if(invisibleView.getVisibility() == View.VISIBLE)
		{
			invisibleView.setVisibility(GONE);
			this.setVisibility(VISIBLE);
			flipping = true;
			next = true;	
			drawtemp1 = invisiblePage;
			drawtemp2 = invisiblePage2;
			flip(invisiblePage, invisiblePage2, invisibleView, invisibleView2);
			count = 2;
		}
		else
		if(invisibleView2.getVisibility() == View.VISIBLE)
		{
			invisibleView2.setVisibility(GONE);
			this.setVisibility(VISIBLE);
			flipping = true;
			next = true;	
			drawtemp1 = invisiblePage2;
			drawtemp2 = visiblePage;
			flip(invisiblePage2, visiblePage, invisibleView2, visibleView);
			count = 0;
		}
		else
		{
			count = 0;
		}
		
	}
	//
	public void pre()
	{
//		swap2Page();
//		visibleView.setVisibility(GONE);
//		this.setVisibility(VISIBLE);
//		flipping = true;
//		next = false;
//		xTouchValue = width-30;
//		flip();
	}
	

	//
//	@Override
//	public boolean onTouchEvent(MotionEvent event) 
//	{
//		// TODO Auto-generated method stub
//
//		if (!onloading) {
//			//
//			switch (event.getAction()) {
//			case MotionEvent.ACTION_DOWN:
//
//				oldTouchX = event.getX();
//				oldTouchY = event.getY();
//				flip = true;
//				if (oldTouchX > (width >> 1)) {
//					xTouchValue = DEFAULT_FLIP_VALUE;
//					yTouchValue = DEFAULT_FLIP_VALUE;
//					// set invisible page's content
//					//
//					next = true;
//				} else {
//					// set invisible page's content
//					// invisiblePage.setContent(index-1, null);
//					//
//					next = false;
//					//
//					swap2Page();
//					xTouchValue = width;
//					yTouchValue = DEFAULT_FLIP_VALUE;
//				}
//				break;
//			case MotionEvent.ACTION_UP:
//				if (onMoving) {
//					xTouchValue = width - A.x;
//					onMoving = false;
//				}
//				flipping = true;
//				flip();
//				break;
//			case MotionEvent.ACTION_MOVE:
//				// Log.i("Thong bao: ","x="+x+" y="+y);
//				onMoving = true;
//				//
//				float xMouse = event.getX();
//				float yMouse = event.getY();
//				//
//				xTouchValue -= (xMouse - oldTouchX) / 1;
//				//
//				yTouchValue -= yMouse - oldTouchY;
//				//
//
//				if (xMouse < oldTouchX) {
//					if (!next) {
//						flip = false;
//					}
//					next = true;
//
//				} else {
//					if (next) {
//						flip = false;
//					}
//					next = false;
//				}
//
//				//
//				oldTouchX = event.getX();
//				oldTouchY = event.getY();
//
//				this.invalidate();
//
//				break;
//			}
//
//		}
//
//		return true;
//	}

	//

	//
	public void flip(Bitmap vp1, Bitmap vp2, View vv1, View iv2) 
	{
		// neu chua load thi load va lat sang trang
		//
		if (flipping) 
		{

			// if (now - mLastMove > mMoveDelay)
			{
			if (xTouchValue > width || xTouchValue < DEFAULT_FLIP_VALUE) 
			{
				flipping = false;
				if (!flipping) 
				{
					//
					if (next) 
					{

						swap2Page(vp1, vp2);

					}
					flip = false;
					xTouchValue = DEFAULT_FLIP_VALUE;
					yTouchValue = DEFAULT_FLIP_VALUE;
					swap2View(vv1,iv2);

				}
				return;
			}
			if (next) 
			{
				// new cd sang trai-> trang moi
				xTouchValue += FLIP_SPEED;
			} 
			else 
			{
				// neu cd sang phai-> trang cu
				xTouchValue -= FLIP_SPEED;

			}
			this.invalidate();
			// }
			// call hander
			flippingHandler.sleep(mMoveDelay);
		}
	  }
	}

	//

	@Override
	protected void onDraw(Canvas canvas) 
	{

		// super.onDraw(canvas);
		width = getWidth();
		height = getHeight();

		//
		if (flipping) 
		{
			pointGenerate(xTouchValue, width, height);
		} 
		else 
		{
			// pointGenerateII(xTouchValue, yTouchValue, width, height);
			pointGenerate(xTouchValue, width, height);
		}
		// First Page render
		canvas.drawColor(Color.GRAY);
		canvas.drawBitmap(drawtemp1, 0, 0, paint);

		// Second Page Render
		Path pathX = pathOfTheMask();
		canvas.clipPath(pathX);
		canvas.drawBitmap(drawtemp2, 0, 0, paint);
		canvas.restore();
		// Flip Page render

		Path pathX2 = pathOfFlippedPaper();
		canvas.drawPath(pathX2, flipPagePaint);
		//
		pathX = null;
		pathX2 = null;
		paint = null;
	}

	// float degress =0;
	//
	private Path pathOfTheMask() 
	{
		Path path = new Path();
		path.moveTo(A.x, A.y);
		path.lineTo(B.x, B.y);
		path.lineTo(C.x, C.y);
		path.lineTo(D.x, D.y);
		path.lineTo(A.x, A.y);
		//

		return path;
	}

	//
	private Path pathOfFlippedPaper() 
	{
		Path path = new Path();
		path.moveTo(A.x, A.y);
		path.lineTo(D.x, D.y);
		path.lineTo(E.x, E.y);
		path.lineTo(F.x, F.y);
		path.lineTo(A.x, A.y);
		return path;
	}

	//
	//
	private void pointGenerate(float distance, int width, int height) 
	{
		float xA = width - distance;
		float yA = height;

		// float xB= width;
		// float yB= height;

		// float xC = width;
		// float yC = 0;

		float xD = 0;
		float yD = 0;
		//
		if (xA > width / 2) {
			xD = width;
			yD = height - (width - xA) * height / xA;
		} else {
			xD = 2 * xA;
			yD = 0;
		}
		//

		//
		double a = (height - yD) / (xD + distance - width);
		double alpha = Math.atan(a);
		double _cos = Math.cos(2 * alpha), _sin = Math.sin(2 * alpha);
		// E
		float xE = (float) (xD + _cos * (width - xD));
		float yE = (float) -(_sin * (width - xD));
		// F
		float xF = (float) (width - distance + _cos * distance);
		float yF = (float) (height - _sin * distance);
		//
		if (xA > width / 2) {
			xE = xD;
			yE = yD;
		}
		//
		A.x = xA;
		A.y = yA;
		D.x = xD;
		D.y = yD;
		E.x = xE;
		E.y = yE;
		F.x = xF;
		F.y = yF;
	}

	//
	float oldxF = 0, oldyF = 0;

	private void pointGenerateII(float xTouch, float yTouch, int width, int height) {
		//
		float yA = height;
		float xD = width;
		//

		//
		float xF = width - xTouch + 0.1f;
		float yF = height - yTouch + 0.1f;
		//
		if (A.x == 0) {
			xF = Math.min(xF, oldxF);
			yF = Math.max(yF, oldyF);
		}
		//
		float deltaX = width - xF;
		float deltaY = height - yF;
		//
		float BH = (float) (Math.sqrt(deltaX * deltaX + deltaY * deltaY) / 2);
		double tangAlpha = deltaY / deltaX;
		double alpha = Math.atan(tangAlpha);
		double _cos = Math.cos(alpha), _sin = Math.sin(alpha);
		//
		float xA = (float) (width - (BH / _cos));
		float yD = (float) (height - (BH / _sin));
		//

		xA = Math.max(0, xA);
		if (xA == 0) {
			// yF= Math.max(yF, height-(float) Math.sqrt(width*width-xF*xF));
			oldxF = xF;
			oldyF = yF;
		}
		//
		float xE = xD;
		float yE = yD;
		// if (xA > width / 2) {

		if (yD < 0) {
			xD = width + (float) (tangAlpha * yD);
			yE = 0;
			xE = width + (float) (Math.tan(2 * alpha) * yD);

		}
		//

		//
		A.x = xA;
		A.y = yA;
		D.x = xD;
		D.y = Math.max(0, yD);
		E.x = xE;
		E.y = yE;
		F.x = xF;
		F.y = yF;
	}

	//
	private void swap2Page(Bitmap vp1, Bitmap vp2) 
	{
		Bitmap temp = vp1;
		vp1 = vp2;
		vp2 = temp;
		// stringSplitterInOrder();
		temp = null;
	}
	private void swap2View(View vv1, View iv2)
	{
		View t = vv1;
		vv1 = iv2;
		iv2 = t;
		t=null;
		//
		vv1.setVisibility(VISIBLE);
		this.setVisibility(GONE);
	}

}
